#include <iostream>

#include <GL/glut.h>

#include "triangle.h"
//#include "objobject.h"
#include "vertex.h"
#include "vtexcoord.h"
#include "common.h"

Triangle::Triangle() {

}

Triangle::Triangle(int vert1, int vert2, int vert3, OBJObject *ownerOBJ) {
	m_ownerOBJ = ownerOBJ;
	if (m_ownerOBJ == NULL) ERROR("Triangle Conctructor: no owner object address found!");
	LOG("vert1 index = " << vert1);
	Vertex *tempVert1 =  m_ownerOBJ->getVertex(vert1);
	LOG("tempVert1: " << tempVert1->m_x << " " << tempVert1->m_y << " " << tempVert1->m_z);
	m_x1 = tempVert1->m_x; m_y1 = tempVert1->m_y; m_z1 = tempVert1->m_z;

	LOG("vert2 index = " << vert2);
	Vertex *tempVert2 =  m_ownerOBJ->getVertex(vert2);
	LOG("tempVert2: " << tempVert2->m_x << " " << tempVert2->m_y << " " << tempVert2->m_z);
	m_x2 = tempVert2->m_x; m_y2 = tempVert2->m_y; m_z2 = tempVert2->m_z;

	LOG("vert3 index = " << vert3);
	Vertex *tempVert3 =  m_ownerOBJ->getVertex(vert3);
	LOG("tempVert3: " << tempVert3->m_x << " " << tempVert3->m_y << " " << tempVert3->m_z);
	m_x3 = tempVert3->m_x; m_y3 = tempVert3->m_y; m_z3 = tempVert3->m_z;
}

Triangle::~Triangle() {

}

void Triangle::addNormals(int vnorm1, int vnorm2, int vnorm3) {
	VertexNormal *tempvNormal = m_ownerOBJ->getNormal(vnorm1);
	m_normalx1 = tempvNormal->m_normalx;
	m_normaly1 = tempvNormal->m_normaly;
	m_normalz1 = tempvNormal->m_normalz;
	
	tempvNormal = m_ownerOBJ->getNormal(vnorm2);
	m_normalx2 = tempvNormal->m_normalx;
	m_normaly2 = tempvNormal->m_normaly;
	m_normalz2 = tempvNormal->m_normalz;
	
	tempvNormal = m_ownerOBJ->getNormal(vnorm3);
	m_normalx3 = tempvNormal->m_normalx;
	m_normaly3 = tempvNormal->m_normaly;
	m_normalz3 = tempvNormal->m_normalz;
}

void Triangle::addTexCoords(int vt1, int vt2, int vt3) {
	VertexTexCoord *tempvt = m_ownerOBJ->getTexCoord(vt1);
	m_texcoordu1 = tempvt->m_x;
	m_texcoordv1 = tempvt->m_y;
	
	tempvt = m_ownerOBJ->getTexCoord(vt2);
	m_texcoordu2 = tempvt->m_x;
	m_texcoordv2 = tempvt->m_y;

	tempvt = m_ownerOBJ->getTexCoord(vt3);
	m_texcoordu3 = tempvt->m_x;
	m_texcoordv3 = tempvt->m_y;
}

void Triangle::drawTriangle() {
//	std::cout << " drawing triangle:\n";
//	std::cout << "  Vertex1: " << m_x1 << " " << m_y1 << " " << m_z1 << "\n";
//	std::cout << "  Vertex2: " << m_x2 << " " << m_y2 << " " << m_z2 << "\n";
//	std::cout << "  Vertex3: " << m_x3 << " " << m_y3 << " " << m_z3 << "\n";
	glBegin(GL_TRIANGLES);
//		glColor3f(0.0f, 1.0f, 0.0f);
		glColor3f(1.0f, 1.0f, 1.0f);
	//	std::cout << "adding normals: " << m_normalx1 << " " << m_normaly1 << " " << m_normalz1 << "\n";
		glNormal3f(m_normalx1, m_normaly1, m_normalz1);
		glTexCoord2f(m_texcoordu1, m_texcoordv1);
//		glVertex3f(m_x1 * 0.25, m_y1 * 0.25, m_z1 * 0.25);
		glVertex3f(m_x1, m_y1, m_z1);
		
//		glColor3f(0.0f * 0.25, 0.0f * 0.25, 1.0f * 0.25);
		glColor3f(1.0f, 1.0f, 1.0f);
		glNormal3f(m_normalx2, m_normaly2, m_normalz2);
		glTexCoord2f(m_texcoordu2, m_texcoordv2);
//		glVertex3f(m_x2 * 0.25, m_y2 * 0.25, m_z2 * 0.25);
		glVertex3f(m_x2, m_y2, m_z2);
		
//		glColor3f(0.0f, 0.5f, 0.5f);
		glColor3f(1.0f, 1.0f, 1.0f);
		glNormal3f(m_normalx3, m_normaly3, m_normalz3);
		glTexCoord2f(m_texcoordu3, m_texcoordv3);
//		glVertex3f(m_x3 * 0.25, m_y3 * 0.25, m_z3 * 0.25);
		glVertex3f(m_x3, m_y3, m_z3);
	glEnd();
	
	//draw normals
/*	
 	glBegin(GL_LINES);
		glColor3f(1.0f, 0.0f, 1.0f);
		glVertex3f(m_x1, m_y1, m_z1);
		glVertex3f(m_x1 + m_normalx1, m_y1 + m_normaly1, m_z1 + m_normalz1);
		
		glColor3f(1.0f, 0.0f, 1.0f);
		glVertex3f(m_x2, m_y2, m_z2);
		glVertex3f(m_x2 + m_normalx2, m_y2 + m_normaly2, m_z2 + m_normalz2);
		
		glColor3f(1.0f, 0.0f, 1.0f);
		glVertex3f(m_x3, m_y3, m_z3);
		glVertex3f(m_x3 + m_normalx3, m_y3 + m_normaly3, m_z3 + m_normalz3);

	glEnd();
*/
}
